package com.smile.chatview;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;

import sun.swing.SwingUtilities2;

public class OvalButton extends BasicButtonUI {

	private Color color;

	public OvalButton(Color color, boolean need) {
		super();
		this.color = color;
	}

	public void installUI(JComponent c) {
		super.installUI(c);

		AbstractButton button = (AbstractButton) c;
		button.setRolloverEnabled(true);
		button.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	}

	@Override
	public void paint(Graphics g, JComponent c) {

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(c.getBackground());
		g2d.fillRect(0, 0, c.getWidth(), c.getHeight());
		super.paint(g, c);
	}

	@Override
	protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
		super.paintIcon(g, c, iconRect);
	}

	@Override
	protected void paintText(Graphics g, JComponent c, Rectangle textRect,
			String text) {
		AbstractButton b = (AbstractButton) c;
		ButtonModel model = b.getModel();
		FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
		int mnemonicIndex = b.getDisplayedMnemonicIndex();

		/* Draw the Text */
		if (model.isEnabled()) {
			/*** paint the text normally */
			if (model.isPressed()) {
				g.setColor(Color.WHITE);
			} else {
				g.setColor(new Color(160, 161, 162));
			}
			SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
					mnemonicIndex, textRect.x + getTextShiftOffset(),
					textRect.y + fm.getAscent() + getTextShiftOffset());
		} else {
			/*** paint the text disabled ***/
			g.setColor(b.getBackground().brighter());
			SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
					mnemonicIndex, textRect.x, textRect.y + fm.getAscent());
			g.setColor(b.getBackground().darker());
			SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
					mnemonicIndex, textRect.x - 1, textRect.y + fm.getAscent()
							- 1);
		}

	}

	@Override
	protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
			String text) {
		super.paintText(g, b, textRect, text);
	}

	@Override
	protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
			Rectangle textRect, Rectangle iconRect) {
		super.paintFocus(g, b, viewRect, textRect, iconRect);
	}

	@Override
	protected void paintButtonPressed(Graphics g, AbstractButton b) {

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(color);
		// if (need) {
		// g2d.fillOval(b.getWidth() - 20, 0, 20, 20);
		// int[] xs = { 0, b.getWidth() - 10, b.getWidth() - 10,
		// b.getWidth(), b.getWidth(), 0, 0 };
		// int[] ys = { 0, 0, 10, 10, b.getHeight(), b.getHeight(), 0 };
		// int ns = 7;
		// g2d.fillPolygon(xs, ys, ns);
		// } else {
		//
		g2d.fillRect(0, 0, b.getWidth(), b.getHeight());
		// }

		super.paintButtonPressed(g, b);
	}

}
